package com.htgd.gardenexpo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConLeisurePrize;
import com.htgd.gardenexpo.dao.GeConLeisurePrizeMapper;
import com.htgd.gardenexpo.page.PrizeQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.IGeConLeisurePrizeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.GeConCompetitionPrizeVO;
import com.htgd.gardenexpo.vo.GeConLeisurePrizeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 挑战奖品 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-27
 */
@Service
public class GeConLeisurePrizeServiceImpl extends ServiceImpl<GeConLeisurePrizeMapper, GeConLeisurePrize> implements IGeConLeisurePrizeService {

    @Autowired
    private GeConLeisurePrizeMapper geConLeisurePrizeMapper;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Override
    public PageInfo<GeConLeisurePrizeVO> pageList(PrizeQuery prizeQuery) {
        PageHelper.startPage(prizeQuery.getPageNum(), prizeQuery.getPageSize());
        List<GeConLeisurePrizeVO> list= geConLeisurePrizeMapper.pageList(prizeQuery);
        for (GeConLeisurePrizeVO geConLeisurePrizeVO : list) {
            if(geConLeisurePrizeVO.getType()!=null){
                geConLeisurePrizeVO.setTypeText(geBaseDictService.getTextByCodeAndValue("type_prize_competiton",geConLeisurePrizeVO.getType()));
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public CommonResult add(GeConLeisurePrize geConLeisurePrize) {
        //判断一个运动下是否只有一个奖项
        boolean exists = lambdaQuery().eq(GeConLeisurePrize::getSportId, geConLeisurePrize.getSportId()).exists();
        if(exists){
            return CommonResult.error("该挑战下已设置奖项，无需重复设置");
        }else{
            geConLeisurePrizeMapper.insert(geConLeisurePrize);
        }
        return CommonResult.success("添加成功");
    }

    @Override
    public CommonResult edit(GeConLeisurePrize geConLeisurePrize) {
        lambdaUpdate().eq(GeConLeisurePrize::getId,geConLeisurePrize.getId())
                .set(GeConLeisurePrize::getName,geConLeisurePrize.getName())
                .set(GeConLeisurePrize::getPicture,geConLeisurePrize.getPicture())
                .set(GeConLeisurePrize::getType,geConLeisurePrize.getType())
                .set(GeConLeisurePrize::getNum,geConLeisurePrize.getNum())
                .update();
        return CommonResult.success("编辑成功");
    }
}
